package com.suyang.syt.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.suyang.syt.dao.interfaces.GroupDao;
import com.suyang.syt.dao.interfaces.MessageDao;
import com.suyang.syt.dao.interfaces.UserDao;
import com.suyang.syt.dao.interfaces.UserGroupDao;
import com.suyang.syt.dao.interfaces.UserMessageDao;
import com.suyang.syt.model.Message;
import com.suyang.syt.model.User;
import com.suyang.syt.model.UserGroup;
import com.suyang.syt.model.UserMessage;
import com.suyang.syt.model.dto.AjaxObj;
import com.suyang.syt.model.dto.MessageDTO;
import com.suyang.syt.model.dto.PagerEasyUI;
import com.suyang.syt.service.interfaces.MessageSpreadService;
import com.suyang.syt.util.DateFormat;
import com.suyang.syt.util.EmptyUtil;

@Service
public class MessageSpreadServiceImpl implements MessageSpreadService {
	@Autowired
	private MessageDao messageDao;
	@Autowired
	private UserMessageDao userMessageDao;
	@Autowired
	private UserDao userDao;
	@Autowired
	private UserGroupDao userGroupDao;
	@Autowired
	private GroupDao groupDao;

	@Override
	public PagerEasyUI<MessageDTO> listByEasyUI(int page, int rows, String sort, String order, String createDate1, String createDate2, String checkName, String checkStatus, String isTime) {
		StringBuilder hql = new StringBuilder("from Message m where 1=1 ");
		if (EmptyUtil.isNotEmpty(createDate1)) {
			hql.append(" and m.createTime >= '").append(createDate1).append("' ");
		}
		if (EmptyUtil.isNotEmpty(createDate2)) {
			hql.append(" and m.createTime <= '").append(createDate2).append("' ");
		}
		if (EmptyUtil.isNotEmpty(checkName)) {
			hql.append(" and m.groupId = ").append(Long.parseLong(checkName));
		}
		if (EmptyUtil.isNotEmpty(checkStatus) && !checkStatus.equals("-1")) {
			hql.append(" and m.status = ").append(Long.parseLong(checkStatus));
		}
		if (EmptyUtil.isNotEmpty(isTime) && !isTime.equals("-1")) {
			hql.append(" and m.isTime = ").append(Long.parseLong(isTime));
		}
		if (EmptyUtil.isNotEmpty(sort)) {
			hql.append(" order by m.").append(sort);
		}
		if (EmptyUtil.isNotEmpty(order)) {
			hql.append(" ").append(order);
		}
		PagerEasyUI<MessageDTO> easyUIPager = new PagerEasyUI<MessageDTO>();
		List<Message> messages = messageDao.findByEasyUI(hql.toString(), page, rows);

		List<MessageDTO> messageDTOs = new ArrayList<MessageDTO>();
		for (Message message : messages) {
			MessageDTO messageDTO = new MessageDTO(message);
			long id = messageDTO.getGroupId();
			if (id == -1) {
				messageDTO.setGroupName("所有用户");
			} else {
				messageDTO.setGroupName(groupDao.load(id).getName());
			}
			messageDTOs.add(messageDTO);
		}
		easyUIPager.setRows(messageDTOs);
		easyUIPager.setTotal((Long) messageDao.queryByHql(" select count(*) " + hql.substring(0, hql.indexOf("order by"))));
		return easyUIPager;
	}

	@Override
	public AjaxObj deletes(String ids) throws Exception {
		if (EmptyUtil.isEmpty(ids)) {
			return new AjaxObj(0, "ids参数错误");
		}
		userMessageDao.deleteByMessageIds(ids);
		messageDao.deleteByIds(ids);
		return new AjaxObj(1, "删除成功!");
	}

	@Override
	public void addTask(Message message) throws Exception {
		messageDao.add(message);
		if (message.getGroupId() == -1) {
			List<User> users = userDao.list("from User");
			for (User user : users) {
				userMessageDao.add(new UserMessage(user.getId(), user.getUsername(), message.getId(), message.getName(), message.getCreateTime()));
			}
		} else {
			List<UserGroup> userGroups = userGroupDao.listGroupUsers(message.getGroupId());
			for (UserGroup userGroup : userGroups) {
				long userId = userGroup.getUserId();
				userMessageDao.add(new UserMessage(userId, userDao.load(userId).getUsername(), message.getId(), message.getName(), message.getCreateTime()));
			}
		}
	}

	@Override
	public void updateStatus(long id, String timing) throws Exception {
		messageDao.updateStatus(id, timing);
	}

	@Override
	public Message load(long id) throws Exception {
		return messageDao.load(id);
	}

	@Override
	public String updateInitStatus() throws Exception {
		String ids = "";
		List<Message> list = messageDao.list(" from Message m where m.status=0 and m.timing <=? ", DateFormat.dateToString(new Date()));
		if (EmptyUtil.isNotEmpty(list)) {
			for (Message message : list) {
				ids += message.getId() + ",";
				messageDao.updateStatus(message.getId(), message.getTiming());
			}
			return ids.substring(0, ids.length() - 1);
		}
		return ids;
	}

}
